{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75b450e5-82d5-4bce-8f04-69d5a40e76bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6a51d2a1-e45b-4dce-b994-3da72a415417",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = pd.read_csv(\"df_train_drug\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a42196bf-d823-4f27-a037-a42fb9c32f3a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4    8987\n",
      "1    1592\n",
      "2    1260\n",
      "0     814\n",
      "3     188\n",
      "Name: label, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(train_data[\"label\"].value_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79083e50-cbe4-47b8-93c0-c525062ecc02",
   "metadata": {},
   "source": [
    "4,negative\n",
    "3.Int\n",
    "2.Mechanism\n",
    "1.Effect\n",
    "0.Advise"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0f091c3-9aaf-4ecb-9951-a4d337853ba6",
   "metadata": {},
   "source": [
    "### 获取药物间的最短依存路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b5a8730c-0e9c-44ba-9991-bbce4538c3a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import spacy\n",
    "import networkx as nx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b756b9d3-e418-4956-9818-8b2287e0fe13",
   "metadata": {},
   "outputs": [],
   "source": [
    "nlp = spacy.load(\"en_core_web_sm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a098f5d4-a1fc-4aa1-a8ca-260ee89e089f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# doc = nlp(\"JIngbo who dresses a green T-shirt was instructed by Chen.\")\n",
    "doc = nlp(\"drug1 alone had no effect on tyrosine phosphorylation in T24 cells, but dose-dependently inhibits the effects of drug2 when both are added simultaneously.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "472a1b83-7386-4aa4-bb11-e224bf25cb12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('had', 'drug1', 'nsubj')\n",
      "('drug1', 'alone', 'advmod')\n",
      "('had', 'had', 'ROOT')\n",
      "('effect', 'no', 'det')\n",
      "('had', 'effect', 'dobj')\n",
      "('effect', 'on', 'prep')\n",
      "('phosphorylation', 'tyrosine', 'amod')\n",
      "('on', 'phosphorylation', 'pobj')\n",
      "('phosphorylation', 'in', 'prep')\n",
      "('cells', 'T24', 'compound')\n",
      "('in', 'cells', 'pobj')\n",
      "('had', ',', 'punct')\n",
      "('had', 'but', 'cc')\n",
      "('inhibits', 'dose', 'nsubj')\n",
      "('dose', '-', 'punct')\n",
      "('inhibits', 'dependently', 'advmod')\n",
      "('had', 'inhibits', 'conj')\n",
      "('effects', 'the', 'det')\n",
      "('inhibits', 'effects', 'dobj')\n",
      "('effects', 'of', 'prep')\n",
      "('of', 'drug2', 'pobj')\n",
      "('added', 'when', 'advmod')\n",
      "('added', 'both', 'nsubjpass')\n",
      "('added', 'are', 'auxpass')\n",
      "('inhibits', 'added', 'advcl')\n",
      "('added', 'simultaneously', 'advmod')\n",
      "('had', '.', 'punct')\n"
     ]
    }
   ],
   "source": [
    "for token in doc:\n",
    "    print((token.head.text, token.text, token.dep_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "53293356-b119-45ca-acc4-92ef3b6dd8ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "#spacy.displacy.serve(doc, style='dep')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "026218a9-7a99-47fb-a516-e64a09e9205c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shortest path lenth:  5\n",
      "shortest path:  ['drug1', 'had', 'inhibits', 'effects', 'of', 'drug2']\n"
     ]
    }
   ],
   "source": [
    "edges = []\n",
    "for token in doc:\n",
    "    for child in token.children:\n",
    "        edges.append(('{0}'.format(token.lower_),\n",
    "                      '{0}'.format(child.lower_)))\n",
    "graph = nx.Graph(edges)\n",
    "entity1 = \"drug1\".lower()\n",
    "entity2 = 'drug2'.lower()\n",
    "print('shortest path lenth: ',nx.shortest_path_length(graph, source=entity1, target=entity2))\n",
    "print('shortest path: ',nx.shortest_path(graph, source=entity1, target=entity2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd570969-7254-4ce6-8e1f-12fc3e30be2b",
   "metadata": {},
   "source": [
    "### 获取删除负例（negative）的数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5af88f7d-61a0-404c-a8a8-de38e6db7ce2",
   "metadata": {},
   "outputs": [],
   "source": [
    "#根据条件读取某行\n",
    "# 读取第label列中小于4的值\n",
    "train_data_pos = train_data.loc[ train_data.label < 4] #等价于 data5 = data[data.B > 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8ab459ca-331c-45df-83d9-e3232804ee53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>idx</th>\n",
       "      <th>text</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>12</td>\n",
       "      <td>The antimicrobial combinations of &lt;e13&gt; drug1 ...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>13</td>\n",
       "      <td>Synergism was observed when &lt;e13&gt; drug1 &lt;/e13&gt;...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>24</td>\n",
       "      <td>&lt;e13&gt; drug1 &lt;/e13&gt; alone had no effect on tyro...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>25</td>\n",
       "      <td>&lt;e13&gt; drug1 &lt;/e13&gt; alone was found to have no ...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>45</td>\n",
       "      <td>Using in situ hybridization, we observed that ...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12824</th>\n",
       "      <td>12824</td>\n",
       "      <td>Multiple-dose administration of the potent CYP...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12829</th>\n",
       "      <td>12829</td>\n",
       "      <td>Coadministration of single, oral doses of &lt;e10...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12831</th>\n",
       "      <td>12831</td>\n",
       "      <td>Other strong selective CYP3A4 inhibitors such ...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12833</th>\n",
       "      <td>12833</td>\n",
       "      <td>Drugs That Inhibit Both Aldehyde Oxidase and C...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12834</th>\n",
       "      <td>12834</td>\n",
       "      <td>Concomitant administration of &lt;e11&gt; drug1 &lt;/e1...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3854 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         idx                                               text  label\n",
       "12        12  The antimicrobial combinations of <e13> drug1 ...      1\n",
       "13        13  Synergism was observed when <e13> drug1 </e13>...      1\n",
       "24        24  <e13> drug1 </e13> alone had no effect on tyro...      1\n",
       "25        25  <e13> drug1 </e13> alone was found to have no ...      1\n",
       "45        45  Using in situ hybridization, we observed that ...      1\n",
       "...      ...                                                ...    ...\n",
       "12824  12824  Multiple-dose administration of the potent CYP...      2\n",
       "12829  12829  Coadministration of single, oral doses of <e10...      2\n",
       "12831  12831  Other strong selective CYP3A4 inhibitors such ...      2\n",
       "12833  12833  Drugs That Inhibit Both Aldehyde Oxidase and C...      2\n",
       "12834  12834  Concomitant administration of <e11> drug1 </e1...      2\n",
       "\n",
       "[3854 rows x 3 columns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_data_pos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7e9e638c-38aa-4d72-85d2-a09e07d7bfaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "#存为一个新的csv文件\n",
    "train_data_pos.to_csv(\"train_data_pos\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a5e05c0f-ae75-464c-ae0b-40ff1f37f86b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>idx</th>\n",
       "      <th>text</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>The antimicrobial combinations of &lt;e13&gt; drug1 ...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13</td>\n",
       "      <td>13</td>\n",
       "      <td>Synergism was observed when &lt;e13&gt; drug1 &lt;/e13&gt;...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>24</td>\n",
       "      <td>24</td>\n",
       "      <td>&lt;e13&gt; drug1 &lt;/e13&gt; alone had no effect on tyro...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>&lt;e13&gt; drug1 &lt;/e13&gt; alone was found to have no ...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>45</td>\n",
       "      <td>45</td>\n",
       "      <td>Using in situ hybridization, we observed that ...</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3849</th>\n",
       "      <td>12824</td>\n",
       "      <td>12824</td>\n",
       "      <td>Multiple-dose administration of the potent CYP...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3850</th>\n",
       "      <td>12829</td>\n",
       "      <td>12829</td>\n",
       "      <td>Coadministration of single, oral doses of &lt;e10...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3851</th>\n",
       "      <td>12831</td>\n",
       "      <td>12831</td>\n",
       "      <td>Other strong selective CYP3A4 inhibitors such ...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3852</th>\n",
       "      <td>12833</td>\n",
       "      <td>12833</td>\n",
       "      <td>Drugs That Inhibit Both Aldehyde Oxidase and C...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3853</th>\n",
       "      <td>12834</td>\n",
       "      <td>12834</td>\n",
       "      <td>Concomitant administration of &lt;e11&gt; drug1 &lt;/e1...</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3854 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0    idx                                               text  \\\n",
       "0             12     12  The antimicrobial combinations of <e13> drug1 ...   \n",
       "1             13     13  Synergism was observed when <e13> drug1 </e13>...   \n",
       "2             24     24  <e13> drug1 </e13> alone had no effect on tyro...   \n",
       "3             25     25  <e13> drug1 </e13> alone was found to have no ...   \n",
       "4             45     45  Using in situ hybridization, we observed that ...   \n",
       "...          ...    ...                                                ...   \n",
       "3849       12824  12824  Multiple-dose administration of the potent CYP...   \n",
       "3850       12829  12829  Coadministration of single, oral doses of <e10...   \n",
       "3851       12831  12831  Other strong selective CYP3A4 inhibitors such ...   \n",
       "3852       12833  12833  Drugs That Inhibit Both Aldehyde Oxidase and C...   \n",
       "3853       12834  12834  Concomitant administration of <e11> drug1 </e1...   \n",
       "\n",
       "      label  \n",
       "0         1  \n",
       "1         1  \n",
       "2         1  \n",
       "3         1  \n",
       "4         1  \n",
       "...     ...  \n",
       "3849      2  \n",
       "3850      2  \n",
       "3851      2  \n",
       "3852      2  \n",
       "3853      2  \n",
       "\n",
       "[3854 rows x 4 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_pos = pd.read_csv(\"train_data_pos\")\n",
    "data_pos"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c14de59a-04a5-4ea9-9b65-aa9d1c39551e",
   "metadata": {},
   "source": [
    "### 统计四种类型数据的长度分布"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e4958feb-93b6-49b1-bbbf-14b7f67095b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "497dd81a-b067-4d2a-83ae-86045dd1e19c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在训练数据中添加新的句子长度列, 每个元素的值都是对应的句子列的长度\n",
    "data_pos[\"sentence_length\"] = list(map(lambda x: len(x), data_pos[\"text\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bae1f50b-54d0-499c-b822-3a386b7f0bdf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 绘制句子长度列的数量分布图\n",
    "# sns.countplot(\"sentence_length\", data=data_pos)\n",
    "# # 主要关注count长度分布的纵坐标, 不需要绘制横坐标, 横坐标范围通过dist图进行查看\n",
    "# plt.xticks([])\n",
    "# plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "398de009-3a7e-4f4a-9257-d9b148960c53",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import AutoTokenizer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f9016d4a-519e-4de4-9610-8574f2817856",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "00e1b769eb58437594df3487ee0167f7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading (…)okenizer_config.json:   0%|          | 0.00/175 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "E:\\Miniconda3-latest-Windows-x86_64\\lib\\site-packages\\huggingface_hub\\file_download.py:133: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in C:\\Users\\HP\\.cache\\huggingface\\hub. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.\n",
      "To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to see activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development\n",
      "  warnings.warn(message)\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3a2352ea41ca4ae0b6c5cc2c3edb65d8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading (…)lve/main/config.json:   0%|          | 0.00/481 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "37e3f3b69f8b47629893c337c311226d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading (…)olve/main/vocab.json:   0%|          | 0.00/899k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ca6dfe911e544e52bb5d7c514e970342",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading (…)olve/main/merges.txt:   0%|          | 0.00/456k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "059a62cbb2b24cb19a3ed25c222116d2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Downloading (…)cial_tokens_map.json:   0%|          | 0.00/150 [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained(\"minhpqn/bio_roberta-base_pubmed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7812be59-e785-4f22-8545-a2051de12f72",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Synergism was observed when <e13> drug1 </e13> was combined with <e20> drug2 </e20> against Bacillus subtilis and Klebsiella oxytoca.'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_pos[\"text\"].loc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d8b861d8-6eff-4089-b388-749ba2b7744a",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = tokenizer('Synergism was observed when <e13> drug1 </e13> was combined with <e20> drug2 </e20> against Bacillus subtilis and Klebsiella oxytoca.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c39bd755-343d-410a-ac1c-51c59d7eeb5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<s>Synergism was observed when <e13> drug1 </e13> was combined with <e20> drug2 </e20> against Bacillus subtilis and Klebsiella oxytoca.</s>'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenizer.decode(result[\"input_ids\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "acd5443f-5a83-4cc9-b920-157681c9a086",
   "metadata": {},
   "outputs": [],
   "source": [
    "data_pos[\"word_num\"] = list(map(lambda x: len(tokenizer(x)['input_ids']), data_pos[\"text\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "87796c43-6c8c-4a89-a202-2d6703af1846",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>idx</th>\n",
       "      <th>text</th>\n",
       "      <th>label</th>\n",
       "      <th>sentence_length</th>\n",
       "      <th>word_num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>12</td>\n",
       "      <td>12</td>\n",
       "      <td>The antimicrobial combinations of &lt;e13&gt; drug1 ...</td>\n",
       "      <td>1</td>\n",
       "      <td>189</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>13</td>\n",
       "      <td>13</td>\n",
       "      <td>Synergism was observed when &lt;e13&gt; drug1 &lt;/e13&gt;...</td>\n",
       "      <td>1</td>\n",
       "      <td>133</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>24</td>\n",
       "      <td>24</td>\n",
       "      <td>&lt;e13&gt; drug1 &lt;/e13&gt; alone had no effect on tyro...</td>\n",
       "      <td>1</td>\n",
       "      <td>180</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>25</td>\n",
       "      <td>25</td>\n",
       "      <td>&lt;e13&gt; drug1 &lt;/e13&gt; alone was found to have no ...</td>\n",
       "      <td>1</td>\n",
       "      <td>166</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>45</td>\n",
       "      <td>45</td>\n",
       "      <td>Using in situ hybridization, we observed that ...</td>\n",
       "      <td>1</td>\n",
       "      <td>313</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3849</th>\n",
       "      <td>12824</td>\n",
       "      <td>12824</td>\n",
       "      <td>Multiple-dose administration of the potent CYP...</td>\n",
       "      <td>2</td>\n",
       "      <td>189</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3850</th>\n",
       "      <td>12829</td>\n",
       "      <td>12829</td>\n",
       "      <td>Coadministration of single, oral doses of &lt;e10...</td>\n",
       "      <td>2</td>\n",
       "      <td>295</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3851</th>\n",
       "      <td>12831</td>\n",
       "      <td>12831</td>\n",
       "      <td>Other strong selective CYP3A4 inhibitors such ...</td>\n",
       "      <td>2</td>\n",
       "      <td>137</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3852</th>\n",
       "      <td>12833</td>\n",
       "      <td>12833</td>\n",
       "      <td>Drugs That Inhibit Both Aldehyde Oxidase and C...</td>\n",
       "      <td>2</td>\n",
       "      <td>256</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3853</th>\n",
       "      <td>12834</td>\n",
       "      <td>12834</td>\n",
       "      <td>Concomitant administration of &lt;e11&gt; drug1 &lt;/e1...</td>\n",
       "      <td>2</td>\n",
       "      <td>152</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3854 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0    idx                                               text  \\\n",
       "0             12     12  The antimicrobial combinations of <e13> drug1 ...   \n",
       "1             13     13  Synergism was observed when <e13> drug1 </e13>...   \n",
       "2             24     24  <e13> drug1 </e13> alone had no effect on tyro...   \n",
       "3             25     25  <e13> drug1 </e13> alone was found to have no ...   \n",
       "4             45     45  Using in situ hybridization, we observed that ...   \n",
       "...          ...    ...                                                ...   \n",
       "3849       12824  12824  Multiple-dose administration of the potent CYP...   \n",
       "3850       12829  12829  Coadministration of single, oral doses of <e10...   \n",
       "3851       12831  12831  Other strong selective CYP3A4 inhibitors such ...   \n",
       "3852       12833  12833  Drugs That Inhibit Both Aldehyde Oxidase and C...   \n",
       "3853       12834  12834  Concomitant administration of <e11> drug1 </e1...   \n",
       "\n",
       "      label  sentence_length  word_num  \n",
       "0         1              189        50  \n",
       "1         1              133        46  \n",
       "2         1              180        53  \n",
       "3         1              166        53  \n",
       "4         1              313        81  \n",
       "...     ...              ...       ...  \n",
       "3849      2              189        63  \n",
       "3850      2              295        82  \n",
       "3851      2              137        43  \n",
       "3852      2              256        80  \n",
       "3853      2              152        55  \n",
       "\n",
       "[3854 rows x 6 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_pos"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42ab7038-1859-4a4f-af88-8370e3bb0abe",
   "metadata": {},
   "source": [
    "### 对长度大于100的部分求最短路径依赖"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "05905d94-eb8c-43ce-b107-976b8151aea9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>idx</th>\n",
       "      <th>text</th>\n",
       "      <th>label</th>\n",
       "      <th>sentence_length</th>\n",
       "      <th>word_num</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>386</th>\n",
       "      <td>1671</td>\n",
       "      <td>1671</td>\n",
       "      <td>Drugs and other substances demonstrated to be ...</td>\n",
       "      <td>3</td>\n",
       "      <td>578</td>\n",
       "      <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>387</th>\n",
       "      <td>1672</td>\n",
       "      <td>1672</td>\n",
       "      <td>Drugs and other substances demonstrated to be ...</td>\n",
       "      <td>3</td>\n",
       "      <td>578</td>\n",
       "      <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>388</th>\n",
       "      <td>1673</td>\n",
       "      <td>1673</td>\n",
       "      <td>Drugs and other substances demonstrated to be ...</td>\n",
       "      <td>3</td>\n",
       "      <td>566</td>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>389</th>\n",
       "      <td>1674</td>\n",
       "      <td>1674</td>\n",
       "      <td>Drugs and other substances demonstrated to be ...</td>\n",
       "      <td>3</td>\n",
       "      <td>575</td>\n",
       "      <td>148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>390</th>\n",
       "      <td>1675</td>\n",
       "      <td>1675</td>\n",
       "      <td>Drugs and other substances demonstrated to be ...</td>\n",
       "      <td>3</td>\n",
       "      <td>573</td>\n",
       "      <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3750</th>\n",
       "      <td>12464</td>\n",
       "      <td>12464</td>\n",
       "      <td>Although specific studies have not been perfor...</td>\n",
       "      <td>2</td>\n",
       "      <td>438</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3751</th>\n",
       "      <td>12465</td>\n",
       "      <td>12465</td>\n",
       "      <td>Although specific studies have not been perfor...</td>\n",
       "      <td>2</td>\n",
       "      <td>443</td>\n",
       "      <td>136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3752</th>\n",
       "      <td>12466</td>\n",
       "      <td>12466</td>\n",
       "      <td>Although specific studies have not been perfor...</td>\n",
       "      <td>2</td>\n",
       "      <td>441</td>\n",
       "      <td>134</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3753</th>\n",
       "      <td>12467</td>\n",
       "      <td>12467</td>\n",
       "      <td>Although specific studies have not been perfor...</td>\n",
       "      <td>2</td>\n",
       "      <td>441</td>\n",
       "      <td>132</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3754</th>\n",
       "      <td>12468</td>\n",
       "      <td>12468</td>\n",
       "      <td>Although specific studies have not been perfor...</td>\n",
       "      <td>2</td>\n",
       "      <td>442</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>246 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      Unnamed: 0    idx                                               text  \\\n",
       "386         1671   1671  Drugs and other substances demonstrated to be ...   \n",
       "387         1672   1672  Drugs and other substances demonstrated to be ...   \n",
       "388         1673   1673  Drugs and other substances demonstrated to be ...   \n",
       "389         1674   1674  Drugs and other substances demonstrated to be ...   \n",
       "390         1675   1675  Drugs and other substances demonstrated to be ...   \n",
       "...          ...    ...                                                ...   \n",
       "3750       12464  12464  Although specific studies have not been perfor...   \n",
       "3751       12465  12465  Although specific studies have not been perfor...   \n",
       "3752       12466  12466  Although specific studies have not been perfor...   \n",
       "3753       12467  12467  Although specific studies have not been perfor...   \n",
       "3754       12468  12468  Although specific studies have not been perfor...   \n",
       "\n",
       "      label  sentence_length  word_num  \n",
       "386       3              578       149  \n",
       "387       3              578       149  \n",
       "388       3              566       146  \n",
       "389       3              575       148  \n",
       "390       3              573       149  \n",
       "...     ...              ...       ...  \n",
       "3750      2              438       133  \n",
       "3751      2              443       136  \n",
       "3752      2              441       134  \n",
       "3753      2              441       132  \n",
       "3754      2              442       133  \n",
       "\n",
       "[246 rows x 6 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_long = data_pos.loc[data_pos.word_num > 120]\n",
    "data_long"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "fc9d3567-949c-4da6-84a9-40ff5b2ec8c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Drugs and other substances demonstrated to be CYP 3A inhibitors on the basis of clinical studies involving benzodiazepines metabolized similarly to alprazolam or on the basis of in vitro studies with alprazolam or other benzodiazepines (caution is recommended during coadministration with alprazolam ): Available data from clinical studies of benzodiazepines other than alprazolam suggest a possible drug interaction with <e10> drug1 </e10> for the following: diltiazem , <e20> drug2 </e20> , macrolide_antibiotics such as erythromycin and clarithromycin , and grapefruit juice.'"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str1 = data_long.loc[387].text\n",
    "str1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "415f639a-d482-4151-bdd9-db4e97918550",
   "metadata": {},
   "outputs": [],
   "source": [
    "# special_toekn  = [\"<e10>\", \"</e10>\", \"<e11>\", \"</e11>\", \"<e12>\", \"</e12>\", \"<e13>\", \"</e13>\",\n",
    "#                   \"<e20>\", \"</e20>\", \"<e21>\", \"</e21>\", \"<e22>\", \"</e22>\", \"<e23>\",\"</e23>\"]\n",
    "def del_special_tokens(text):\n",
    "    special_tokens = []\n",
    "    special_toekn_1  = [\"<e10>\", \"<e11>\", \"<e12>\", \"<e13>\", \"<e20>\", \"<e21>\", \"<e22>\", \"<e23>\"]\n",
    "    for token in special_toekn_1:\n",
    "        index = text.find(token)\n",
    "        if index != -1:  # 存在该标记\n",
    "            text = text[:index] + text[index + 6:]\n",
    "            special_tokens.append(token)\n",
    "        \n",
    "    special_toekn_2  = [\"</e10>\", \"</e11>\", \"</e12>\", \"</e13>\", \"</e20>\", \"</e21>\", \"</e22>\", \"</e23>\"]\n",
    "    for token in special_toekn_2:\n",
    "        index = text.find(token)  # 存在该标记\n",
    "        if index != -1:\n",
    "            text = text[:index] + text[index + 7:]\n",
    "            special_tokens.append(token)\n",
    "    return text, special_tokens  # 不含有 <> 和 </>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "d1c1a6b2-8d51-4eed-b9bd-56f99380164b",
   "metadata": {},
   "outputs": [],
   "source": [
    "str1, special_toekns = del_special_tokens(str1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "id": "d0eaf52c-2dba-4d2f-9910-a4cf23471afd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Drugs and other substances demonstrated to be CYP 3A inhibitors on the basis of clinical studies involving benzodiazepines metabolized similarly to alprazolam or on the basis of in vitro studies with alprazolam or other benzodiazepines (caution is recommended during coadministration with alprazolam ): Available data from clinical studies of benzodiazepines other than alprazolam suggest a possible drug interaction with drug1 for the following: diltiazem , drug2 , macrolide_antibiotics such as erythromycin and clarithromycin , and grapefruit juice.'"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29bc69ac-b39a-46b4-a58f-679585c236a4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "id": "27275291-4ad4-4bdb-88cc-72e502bd1028",
   "metadata": {},
   "outputs": [],
   "source": [
    "import spacy\n",
    "import networkx as nx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "6a8e94b5-b833-471b-8c8c-94a7c7785f3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "nlp = spacy.load(\"en_core_web_sm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "5a2268c3-ef91-4eab-aa93-5b9e524fa36b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# doc = nlp(\"JIngbo who dresses a green T-shirt was instructed by Chen.\")\n",
    "doc = nlp(str1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "2ea5c623-3a86-4dbd-8fb7-359af533b9b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('demonstrated', 'Drugs', 'nsubj')\n",
      "('Drugs', 'and', 'cc')\n",
      "('substances', 'other', 'amod')\n",
      "('Drugs', 'substances', 'conj')\n",
      "('demonstrated', 'demonstrated', 'ROOT')\n",
      "('be', 'to', 'aux')\n",
      "('demonstrated', 'be', 'xcomp')\n",
      "('3A', 'CYP', 'compound')\n",
      "('inhibitors', '3A', 'compound')\n",
      "('be', 'inhibitors', 'attr')\n",
      "('be', 'on', 'prep')\n",
      "('basis', 'the', 'det')\n",
      "('on', 'basis', 'pobj')\n",
      "('basis', 'of', 'prep')\n",
      "('studies', 'clinical', 'amod')\n",
      "('of', 'studies', 'pobj')\n",
      "('studies', 'involving', 'acl')\n",
      "('involving', 'benzodiazepines', 'dobj')\n",
      "('benzodiazepines', 'metabolized', 'acl')\n",
      "('metabolized', 'similarly', 'advmod')\n",
      "('metabolized', 'to', 'prep')\n",
      "('to', 'alprazolam', 'pobj')\n",
      "('to', 'or', 'cc')\n",
      "('recommended', 'on', 'prep')\n",
      "('basis', 'the', 'det')\n",
      "('on', 'basis', 'pobj')\n",
      "('basis', 'of', 'prep')\n",
      "('vitro', 'in', 'advmod')\n",
      "('studies', 'vitro', 'amod')\n",
      "('of', 'studies', 'pobj')\n",
      "('studies', 'with', 'prep')\n",
      "('with', 'alprazolam', 'pobj')\n",
      "('alprazolam', 'or', 'cc')\n",
      "('benzodiazepines', 'other', 'amod')\n",
      "('alprazolam', 'benzodiazepines', 'conj')\n",
      "('recommended', '(', 'punct')\n",
      "('recommended', 'caution', 'nsubjpass')\n",
      "('recommended', 'is', 'auxpass')\n",
      "('demonstrated', 'recommended', 'conj')\n",
      "('recommended', 'during', 'prep')\n",
      "('during', 'coadministration', 'pobj')\n",
      "('coadministration', 'with', 'prep')\n",
      "('with', 'alprazolam', 'pobj')\n",
      "('recommended', '):', 'punct')\n",
      "('data', 'Available', 'amod')\n",
      "('suggest', 'data', 'nsubj')\n",
      "('data', 'from', 'prep')\n",
      "('studies', 'clinical', 'amod')\n",
      "('from', 'studies', 'pobj')\n",
      "('studies', 'of', 'prep')\n",
      "('of', 'benzodiazepines', 'pobj')\n",
      "('benzodiazepines', 'other', 'amod')\n",
      "('other', 'than', 'prep')\n",
      "('than', 'alprazolam', 'pobj')\n",
      "('demonstrated', 'suggest', 'conj')\n",
      "('interaction', 'a', 'det')\n",
      "('interaction', 'possible', 'amod')\n",
      "('interaction', 'drug', 'compound')\n",
      "('suggest', 'interaction', 'dobj')\n",
      "('interaction', 'with', 'prep')\n",
      "('with', 'drug1', 'pobj')\n",
      "('interaction', 'for', 'prep')\n",
      "('following', 'the', 'det')\n",
      "('for', 'following', 'pobj')\n",
      "('following', ':', 'punct')\n",
      "('following', 'diltiazem', 'appos')\n",
      "('diltiazem', ',', 'punct')\n",
      "('diltiazem', 'drug2', 'conj')\n",
      "('suggest', ',', 'punct')\n",
      "('suggest', 'macrolide_antibiotics', 'npadvmod')\n",
      "('as', 'such', 'amod')\n",
      "('macrolide_antibiotics', 'as', 'prep')\n",
      "('as', 'erythromycin', 'pobj')\n",
      "('erythromycin', 'and', 'cc')\n",
      "('erythromycin', 'clarithromycin', 'conj')\n",
      "('clarithromycin', ',', 'punct')\n",
      "('clarithromycin', 'and', 'cc')\n",
      "('juice', 'grapefruit', 'compound')\n",
      "('clarithromycin', 'juice', 'conj')\n",
      "('suggest', '.', 'punct')\n"
     ]
    }
   ],
   "source": [
    "for token in doc:\n",
    "    print((token.head.text, token.text, token.dep_))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "a0d801df-0b1b-4601-8fe9-2c382fc19f2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shortest path lenth:  6\n",
      "shortest path:  ['drug1', 'with', 'interaction', 'suggest', ',', 'diltiazem', 'drug2']\n"
     ]
    }
   ],
   "source": [
    "edges = []\n",
    "for token in doc:\n",
    "    for child in token.children:\n",
    "        edges.append(('{0}'.format(token.lower_),\n",
    "                      '{0}'.format(child.lower_)))\n",
    "graph = nx.Graph(edges)\n",
    "entity1 = \"drug1\".lower()\n",
    "entity2 = 'drug2'.lower()\n",
    "print('shortest path lenth: ',nx.shortest_path_length(graph, source=entity1, target=entity2))\n",
    "print('shortest path: ',nx.shortest_path(graph, source=entity1, target=entity2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "1174538e-c604-455d-bacf-8b98bc742662",
   "metadata": {},
   "outputs": [],
   "source": [
    "sdp = nx.shortest_path(graph, source=entity1, target=entity2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "9cd3f916-280f-46da-b7ab-54300070aa36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['drug1', 'with', 'interaction', 'suggest', ',', 'diltiazem', 'drug2']"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "775d40e4-73a6-4cbc-80d3-34fb0e0b2407",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['<e10>', '<e20>', '</e10>', '</e20>']"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "special_toekns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "662b7ba1-9185-42ca-9b0b-a4758bd77a57",
   "metadata": {},
   "outputs": [],
   "source": [
    "sdp.insert(0, special_toekns[0])\n",
    "sdp.insert(-1, special_toekns[1])\n",
    "sdp.insert(2, special_toekns[2])\n",
    "sdp.append(special_toekns[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "7b830251-06f8-4aac-9a29-529a70d72f41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['<e10>',\n",
       " 'drug1',\n",
       " '</e10>',\n",
       " 'with',\n",
       " 'interaction',\n",
       " 'suggest',\n",
       " ',',\n",
       " 'diltiazem',\n",
       " '<e20>',\n",
       " 'drug2',\n",
       " '</e20>']"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "323271ed-197a-4a8a-b641-41e07b590c54",
   "metadata": {},
   "outputs": [],
   "source": [
    "str2 = \" \".join(sdp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "7ed9466d-b33e-4ecf-bc9c-a75ef58ddbf9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<e10> drug1 </e10> with interaction suggest , diltiazem <e20> drug2 </e20>'"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e2303cd-c2ae-49c0-a026-1cbf96d4232d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.19"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "0055fdfad4404d1c93a64b36ca7f2167": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "bar_style": "success",
       "layout": "IPY_MODEL_931f79523f2940248cc182ceb3e70e71",
       "max": 175,
       "style": "IPY_MODEL_8685f544d09f45d7bddc9369412fe8ba",
       "value": 175
      }
     },
     "00e1b769eb58437594df3487ee0167f7": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_eb3a10cc7d7941f4b4860085fe248bc3",
        "IPY_MODEL_0055fdfad4404d1c93a64b36ca7f2167",
        "IPY_MODEL_a1fdc49ea1b148919bc078d25fc5d0a0"
       ],
       "layout": "IPY_MODEL_3f6f7e8622f5472a92ebe846c40cfc89"
      }
     },
     "00f22eba47844a21aeb0671b0da9ea55": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "0387940197714d0680ba165df0d1bee1": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "bar_style": "success",
       "layout": "IPY_MODEL_31f8eaba26bc4313a7d2e8ec8460f2cd",
       "max": 898822,
       "style": "IPY_MODEL_b82cf679fcc34ddbb9f5fb0ac8e67215",
       "value": 898822
      }
     },
     "04b4035ccf5e47f69e6d702f403c1bcf": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "059a62cbb2b24cb19a3ed25c222116d2": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_e3df11daf3f74ea6a0012cde8630a2cd",
        "IPY_MODEL_6970639c70ba4227833895e23ca91bd9",
        "IPY_MODEL_fee68a8c50204920980ecc8e707c6c41"
       ],
       "layout": "IPY_MODEL_fb70603fb4ca4f5784991e89fdf53e83"
      }
     },
     "09abb77041324fa7907577595cfc5613": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "0aa0172df7c7420ebcacab4758c138ae": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "18fe74b6ca88439ab6284751deb01e9c": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "bar_style": "success",
       "layout": "IPY_MODEL_9fd992bad23c4aef8be39e8b05464abf",
       "max": 481,
       "style": "IPY_MODEL_38c63ac6190d45f1a4a6833fbf6dfb07",
       "value": 481
      }
     },
     "2fcf687624534be7985f35a40885385f": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "2ffad48b30de479d8f21e462d37168e1": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "31f8eaba26bc4313a7d2e8ec8460f2cd": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "37e3f3b69f8b47629893c337c311226d": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_67b9e277873c48ec929b31d1921b4e9e",
        "IPY_MODEL_0387940197714d0680ba165df0d1bee1",
        "IPY_MODEL_88bd22b7c164452cb6f604c7df5543b3"
       ],
       "layout": "IPY_MODEL_3f301d8437d4452bacaf1e1e33313ea3"
      }
     },
     "37f26b071864400fbb29d54e3728503f": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "38c63ac6190d45f1a4a6833fbf6dfb07": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "3a2352ea41ca4ae0b6c5cc2c3edb65d8": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_b778a8ca8a0b4178b6f1115b59e7b884",
        "IPY_MODEL_18fe74b6ca88439ab6284751deb01e9c",
        "IPY_MODEL_5659ec7ca1ed408dbe57c9965602dc1b"
       ],
       "layout": "IPY_MODEL_40d3d3dd1f744d3c9765d7494129d9b0"
      }
     },
     "3f301d8437d4452bacaf1e1e33313ea3": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "3f6f7e8622f5472a92ebe846c40cfc89": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "40d3d3dd1f744d3c9765d7494129d9b0": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "41eb5486ec6f444eabf266e76fae228b": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "4e93c20c7f084978a89681892bfce8d4": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "5659ec7ca1ed408dbe57c9965602dc1b": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_4e93c20c7f084978a89681892bfce8d4",
       "style": "IPY_MODEL_5b126d93b58e4d1b9086824f0bf67c1c",
       "value": " 481/481 [00:00&lt;00:00, 16.0kB/s]"
      }
     },
     "5b126d93b58e4d1b9086824f0bf67c1c": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "67b9e277873c48ec929b31d1921b4e9e": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_0aa0172df7c7420ebcacab4758c138ae",
       "style": "IPY_MODEL_85d6c12722a748c8bb0137c128013eca",
       "value": "Downloading (…)olve/main/vocab.json: 100%"
      }
     },
     "6970639c70ba4227833895e23ca91bd9": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "bar_style": "success",
       "layout": "IPY_MODEL_09abb77041324fa7907577595cfc5613",
       "max": 150,
       "style": "IPY_MODEL_b7ca37e92c4b43af9e9fd01cadf1ab69",
       "value": 150
      }
     },
     "6f7a82447311401cb5feb2e1155d13df": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "7188f17bb9ba439e85278af778a85c79": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "8020e8b86db24b35bbef2cdfcf2695b4": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "85d6c12722a748c8bb0137c128013eca": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "8685f544d09f45d7bddc9369412fe8ba": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "88bd22b7c164452cb6f604c7df5543b3": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_37f26b071864400fbb29d54e3728503f",
       "style": "IPY_MODEL_41eb5486ec6f444eabf266e76fae228b",
       "value": " 899k/899k [00:00&lt;00:00, 1.40MB/s]"
      }
     },
     "931f79523f2940248cc182ceb3e70e71": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "95b946512dfe4ce7a877c5fb58bedcea": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "97da79ace4ec400083cdb578f47d8272": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "9eb1f420a7e64eb5b248d852594e3792": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "9fd992bad23c4aef8be39e8b05464abf": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "a1fdc49ea1b148919bc078d25fc5d0a0": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_d7065227f9954698a76e4abb5d68a13a",
       "style": "IPY_MODEL_00f22eba47844a21aeb0671b0da9ea55",
       "value": " 175/175 [00:00&lt;00:00, 9.19kB/s]"
      }
     },
     "a809c8d1c6484d09860aadb22c9d96a7": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "b0ca6c725f3f4404a74657fa522d9f8e": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_fe61f91e17cb4c818486a40517a14f78",
       "style": "IPY_MODEL_97da79ace4ec400083cdb578f47d8272",
       "value": "Downloading (…)olve/main/merges.txt: 100%"
      }
     },
     "b0cba274e1de4fd788dc964f3ce8b8e3": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "b778a8ca8a0b4178b6f1115b59e7b884": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_7188f17bb9ba439e85278af778a85c79",
       "style": "IPY_MODEL_def7403af93243ccae80b45e97b96c17",
       "value": "Downloading (…)lve/main/config.json: 100%"
      }
     },
     "b7ca37e92c4b43af9e9fd01cadf1ab69": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "b824a13707244263bf3aef41c224fd4a": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "b82cf679fcc34ddbb9f5fb0ac8e67215": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "ca6dfe911e544e52bb5d7c514e970342": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "children": [
        "IPY_MODEL_b0ca6c725f3f4404a74657fa522d9f8e",
        "IPY_MODEL_d5d9874c61a5412f801ebb91baff5aa0",
        "IPY_MODEL_d5033875229b43bd96815a6c4d4dec40"
       ],
       "layout": "IPY_MODEL_b0cba274e1de4fd788dc964f3ce8b8e3"
      }
     },
     "d12ea4a410b74dfd8f766699fb5ccf9c": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "d5033875229b43bd96815a6c4d4dec40": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_2fcf687624534be7985f35a40885385f",
       "style": "IPY_MODEL_04b4035ccf5e47f69e6d702f403c1bcf",
       "value": " 456k/456k [00:00&lt;00:00, 6.18MB/s]"
      }
     },
     "d5d9874c61a5412f801ebb91baff5aa0": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "bar_style": "success",
       "layout": "IPY_MODEL_b824a13707244263bf3aef41c224fd4a",
       "max": 456318,
       "style": "IPY_MODEL_95b946512dfe4ce7a877c5fb58bedcea",
       "value": 456318
      }
     },
     "d7065227f9954698a76e4abb5d68a13a": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "def7403af93243ccae80b45e97b96c17": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "description_width": ""
      }
     },
     "e3df11daf3f74ea6a0012cde8630a2cd": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_6f7a82447311401cb5feb2e1155d13df",
       "style": "IPY_MODEL_9eb1f420a7e64eb5b248d852594e3792",
       "value": "Downloading (…)cial_tokens_map.json: 100%"
      }
     },
     "eb3a10cc7d7941f4b4860085fe248bc3": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_d12ea4a410b74dfd8f766699fb5ccf9c",
       "style": "IPY_MODEL_8020e8b86db24b35bbef2cdfcf2695b4",
       "value": "Downloading (…)okenizer_config.json: 100%"
      }
     },
     "fb70603fb4ca4f5784991e89fdf53e83": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "fe61f91e17cb4c818486a40517a14f78": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {}
     },
     "fee68a8c50204920980ecc8e707c6c41": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "layout": "IPY_MODEL_a809c8d1c6484d09860aadb22c9d96a7",
       "style": "IPY_MODEL_2ffad48b30de479d8f21e462d37168e1",
       "value": " 150/150 [00:00&lt;00:00, 9.03kB/s]"
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
